--#include "data\functions\Activity\OpenServer\OpenServerCommon.lua" once
--开服全民BOSS

function OpenServerBossDie( monster,sysarg,monId )
	
	local sVar = Actor.getStaticVar(sysarg)


	if not sVar.openServerBossData then
		sVar.openServerBossData = {}
	end
	local findBossState = false
	for idx, data in ipairs(OpenServerCfg.Boss) do
		for bossIndex, bossId  in ipairs(data.bossIds) do
			if bossId == monId then
				findBossState = true
				if not sVar.openServerBossData[idx] then
					sVar.openServerBossData[idx] = {}
				end
				local killValue = sVar.openServerBossData[idx].killFlag or 0
				local newValue = System.setIntBit(killValue, bossIndex-1, true)
				sVar.openServerBossData[idx].killFlag = newValue
				local getValue = sVar.openServerBossData[idx].getFlag or 0
				local State = 1
				for bossIdx= 1, #data.bossIds do
					local bitValue = System.getIntBit(newValue, bossIdx-1)
					if bitValue == 0 then
						State = 0
						break
					end
				end
				if getValue < State then
					getValue = State
				end
				sVar.openServerBossData[idx].getFlag = getValue
				break
			end
		end
		if findBossState then
			break
		end
	end
	if findBossState then
		OpenServerSendBossInfo(sysarg)
	end
end

function OpenServerSendBossInfo(sysarg)

	local sVar = Actor.getStaticVar(sysarg)


	if not sVar.openServerBossData then
		sVar.openServerBossData = {}
	end

	local netPack = DataPack.allocPacket(sysarg,LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sSendOpenServerKillBossInfo)
	if netPack then
		local length = #OpenServerCfg.Boss
		DataPack.writeChar(netPack, length)
		for idx, data in ipairs(OpenServerCfg.Boss) do
			if not sVar.openServerBossData[idx] then
				sVar.openServerBossData[idx] = {}
			end
			local killValue = sVar.openServerBossData[idx].killFlag or 0
			DataPack.writeChar(netPack, killValue)
			local getValue = sVar.openServerBossData[idx].getFlag or 0
			DataPack.writeChar(netPack, getValue)
		end
		DataPack.flush(netPack) 
	end

end

function OpenServerGetBossDieAward(sysarg, packet)

	local idx = DataPack.readChar(packet)

	local bossCfg = OpenServerCfg.Boss
	
	local BossConfig = bossCfg[idx]

	if not BossConfig then
		return
	end

	local sVar = Actor.getStaticVar(sysarg)


	if not sVar.openServerBossData then
		return 
	end

	if not sVar.openServerBossData[idx] then
		return 
	end


	if not sVar.openServerBossData[idx].getFlag or sVar.openServerBossData[idx].getFlag ~= 1 then
		return 
	end

	if CheckBagGridForAwardsEx(sysarg, BossConfig.awards) > 0 then
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.COM0015, ttFlyTip )
		return
	end

	sVar.openServerBossData[idx].getFlag = 2
	

	GiveCommonAwardEx(sysarg, BossConfig.awards, 1096, Lang.LogTips.OpenServerBoss)

	OpenServerSendBossInfo(sysarg)
end


